package com.douqu.game.core.util;

import com.douqu.game.core.controller.PlayerController;
import com.douqu.game.core.e.ReturnMessage;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.protobuf.SGMainProto;
import com.douqu.game.core.protobuf.SGSystemProto;
import com.douqu.game.core.web.response.BaseResponseDto;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;

import java.nio.ByteOrder;

/**
 * Created by bean
 * 2017-03-24 15:54.
 */
public class SendUtils {

    public static void sendMsg(Channel channel, int msgID, byte[] data)
    {
        ByteBuf byteBuf = Unpooled.buffer();
        int length = data == null ? 0 : data.length;
        if(ByteOrder.nativeOrder().toString().equals(ByteOrder.LITTLE_ENDIAN.toString())){
            byteBuf.writeIntLE(length);//总包长
            byteBuf.writeShortLE(msgID);//长度为2
        }else{
            byteBuf.writeInt(length);//总包长
            byteBuf.writeShort(msgID);//长度为2
        }
        if(data != null)
            byteBuf.writeBytes(data);

        channel.writeAndFlush(byteBuf);
    }

    public static void sendAlert(PlayerController playerController, SGCommonProto.E_NOTIFY_TYPE notifyResult)
    {
        if(playerController == null)
            return;

        SGSystemProto.S2C_NotifyAlert.Builder response = SGSystemProto.S2C_NotifyAlert.newBuilder();
        response.setType(notifyResult);
        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_System_NotifyAlert_VALUE, response.build().toByteArray());
    }



    public static BaseResponseDto createMsg(){

        return new BaseResponseDto(ReturnMessage.SUCCESS);
    }

    public static <T> BaseResponseDto createMsg(T data){

        return new BaseResponseDto(ReturnMessage.SUCCESS, data);
    }

    public static BaseResponseDto createMsg(ReturnMessage error){

        return new BaseResponseDto(error);
    }


}
